//
//  GaoTimeManager.m
//
//  Created by Gikki Ares on 2021/3/31.
//

#import "JsTimeManager.h"

@interface JsTimeManager()


@end

static NSDateFormatter * df_minStd;
static NSDateFormatter * df_std;
static NSDateFormatter * df_stdDate;

@implementation JsTimeManager

+ (void)load {
	df_minStd = [[NSDateFormatter alloc]init];
	df_minStd.dateFormat = @"yyyyMMddHHmmss";
	
	df_std = [[NSDateFormatter alloc]init];
	df_std.dateFormat = @"yyyy-MM-dd HH:mm:ss";
	
	df_stdDate = [[NSDateFormatter alloc]init];
	df_stdDate.dateFormat = @"yyyy-MM-dd";
}

+ (NSString *)
stdTimeStringInmmssForMiniSeconds:(int)miniSeconds {
	int seconds = miniSeconds/1000.0f;
	return [self stdTimeStringInmmssForSeconds:seconds];
}

+ (NSString *)stdTimeStringInmmssForSeconds:(int)seconds {
	int min = seconds/60;
	int sec = seconds - min * 60;
	NSString * str = [NSString stringWithFormat:@"%02d:%02d",min,sec];
	return str;
}

//hh:mm:ss格式的时间字符串.
+ (NSString *) stdTimeStringInhhmmssForSeconds:(int)seconds {
	int hour = seconds/3600;
	int leftSec = seconds - hour * 3600;
	
	int min = leftSec/60;
	int sec = leftSec - min * 60;
	NSString * str = [NSString stringWithFormat:@"%02d:%02d:%02d",hour,min,sec];
	return str;
}

+ (NSString *) timeStringWithSeconds:(float)seconds {
	int min = seconds/60;
	float sec = seconds - min * 60;
	NSString * str = [NSString stringWithFormat:@"%02d:%02.0f",min,sec];
	return str;
}

+ (NSString *)minTimeStringForNow {
	NSDate * date =[NSDate new];
	return [df_minStd stringFromDate:date];
}

+ (NSString *)stdTimeStringForNow {
	NSDate * date =[NSDate new];
	return [df_std stringFromDate:date];
}

+ (NSString *)descriptinForTimeStringDifNow:(NSString *)timeString {
	
	NSDate * date = [df_std dateFromString:timeString];
	
	
	int interval = abs((int)[date timeIntervalSinceNow]);
	
	int min = (interval) / 60;
	int sec = (interval) % 60;
	
	if(min == 0) {
		if(sec < 10) {
			//小于10秒.
			return [NSString stringWithFormat:@"%@",@"刚刚"];
		}
		else {
		//小于1分钟
			return [NSString stringWithFormat:@"%d秒前",sec];
		}
	}
	else {
		//大于1分钟
		
		if(min<60) {
		
			return [NSString stringWithFormat:@"%d分钟前",min];
		}
		else {
			//大于1h
			int hour = min/60;
			
			if(hour<24) {
				return [NSString stringWithFormat:@"%d小时前",hour];
			}
			else {
				//大于1天
				int day = hour/24;
				if(day<3) {
					return [NSString stringWithFormat:@"%d天前",day];
				}
				else {
					//MM-DD
					return [timeString substringWithRange:NSMakeRange(5,5)];
				}
			}
			
			
		}
	}
	
	//获取当前时间的
	return timeString;
}


+ (BOOL)isStdDateTimeStringInToday:(NSString *)dateTimeString {
	NSString * dateString = [dateTimeString substringWithRange:NSMakeRange(0, 10)];
	return [self isStdDateStringInToday:dateString];
}
+ (BOOL)isStdDateStringInToday:(NSString *)dateString {
	NSString * string = [df_stdDate stringFromDate:[NSDate date]];
	if([dateString isEqualToString:string]) {
		return YES;
	}
	else {
		return NO;
	}
}

@end
